﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration; 
using System.Data.SqlClient;

namespace LOGIN
{
    public partial class PriceChange : Form
    {
        public PriceChange()
        {
            InitializeComponent();
        }

        private void PriceChange_Load(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT MedicineID,DrugName,Guige,IndPrice,OutPrice FROM medicineData;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable PriceChangeTable = new DataTable();
            sqlDataAdapter.Fill(PriceChangeTable);
            this.dgv_PriceChangeTable.DataSource = PriceChangeTable;
            dgv_PriceChangeTable.Columns["MedicineID"].HeaderText = "ID";
            dgv_PriceChangeTable.Columns["DrugName"].HeaderText = "药品名";
            dgv_PriceChangeTable.Columns["Guige"].HeaderText = "规格";
            dgv_PriceChangeTable.Columns["IndPrice"].HeaderText = "入库价格";
            dgv_PriceChangeTable.Columns["OutPrice"].HeaderText = "出库价格";
        
        
        
        }
        

        int top = 0;
        int left = 0;
        int height = 0;
        int width1 = 0;

        private void dgv_PriceChangeTable_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            
            DataGridView dgv = (DataGridView)(sender);
            if (e.RowIndex == -1 && (e.ColumnIndex == 3 || e.ColumnIndex == 4)) 
            {

                
                if (e.ColumnIndex == 3)
                {
                    top = e.CellBounds.Top;
                    left = e.CellBounds.Left;
                    height = e.CellBounds.Height;
                    width1 = e.CellBounds.Width;
                }


                int width2 = this.dgv_PriceChangeTable.Columns[4].Width;

                Rectangle rect = new Rectangle(left, top, width1 + width2, e.CellBounds.Height);
                using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    
                    e.Graphics.FillRectangle(backColorBrush, rect);
                }

                using (Pen gridLinePen = new Pen(dgv.GridColor))
                {
                    e.Graphics.DrawLine(gridLinePen, left, top, left + width1 + width2, top);
                    e.Graphics.DrawLine(gridLinePen, left, top + height / 2, left + width1 + width2, top + height / 2);
                    e.Graphics.DrawLine(gridLinePen, left + width1, top + height / 2, left + width1, top + height);

                    
                    string columnValue = "价格";
                    SizeF sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
                    float lstr = (width1 + width2 - sf.Width) / 2;
                    float rstr = (height / 2 - sf.Height) / 2;
                    

                    if (columnValue != "")
                    {
                        e.Graphics.DrawString(columnValue, e.CellStyle.Font,
                                                   new SolidBrush(e.CellStyle.ForeColor),
                                                     left + lstr,
                                                     top + rstr,
                                                     StringFormat.GenericDefault);
                    }


                    
                    columnValue = "入库价格";
                    sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
                    lstr = (width1 - sf.Width) / 2;
                    rstr = (height / 2 - sf.Height) / 2;
                    

                    if (columnValue != "")
                    {
                        e.Graphics.DrawString(columnValue, e.CellStyle.Font,
                                                   new SolidBrush(e.CellStyle.ForeColor),
                                                     left + lstr,
                                                     top + height / 2 + rstr,
                                                     StringFormat.GenericDefault);
                    }


                    
                    columnValue = "出库价格";
                    sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
                    lstr = (width2 - sf.Width) / 2;
                    rstr = (height / 2 - sf.Height) / 2;
                    

                    if (columnValue != "")
                    {
                        e.Graphics.DrawString(columnValue, e.CellStyle.Font,
                                                   new SolidBrush(e.CellStyle.ForeColor),
                                                     left + width1 + lstr,
                                                     top + height / 2 + rstr,
                                                     StringFormat.GenericDefault);
                    }

                }
                e.Handled = true;
            }
        
        }

        private void dgv_PriceChangeTable_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_PriceChangeTable.SelectedCells.Count != 0)
            {
                int index = dgv_PriceChangeTable.CurrentRow.Index;
                this.Medicine.Text = dgv_PriceChangeTable.Rows[index].Cells[1].Value.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
            SqlCommand updateCommand = new SqlCommand();
            updateCommand.Connection = sqlConnection;
            updateCommand.CommandText =
                "UPDATE medicineData"
                + " SET IndPrice=@IndPrice,OutPrice=@OutPrice"
                + " WHERE MedicineID=@MedicineID;";
            updateCommand.Parameters.Add("@OutPrice", SqlDbType.Char, 10, "OutPrice");
            updateCommand.Parameters.Add("@IndPrice", SqlDbType.Int, 0, "IndPrice");
            updateCommand.Parameters.Add("@MedicineID", SqlDbType.Int, 0, "MedicineID");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           
            sqlDataAdapter.UpdateCommand = updateCommand;                                                  
            DataTable studentTable1 = (DataTable)this.dgv_PriceChangeTable.DataSource;                             
            sqlConnection.Open();                                                                          
            int rowAffected = sqlDataAdapter.Update(studentTable1);                                       
            sqlConnection.Close();                                                                        
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");
        }


       
    }
}
